package diandiansong.library.ui.base;

import android.support.annotation.NonNull;
import android.view.View;
import android.widget.AdapterView;

import java.util.List;

/**
 * Created by act on 2018/3/26.
 */

public abstract class FastListFragment<T> extends BaseListFragment<T> {

    public abstract int getItemLayoutRes();

    public abstract void bindItemView(int position, @NonNull View convertView);

    /**
     * viewCreated 后调用
     *
     * @param rootView framentView
     */
    @Override
    public void initView(View rootView) {
    }

    /**
     * 初始化adapter
     *
     * @return adapter
     */
    @Override
    protected BaseListAdapter initAdapter() {
        return new BaseListAdapter(getActivity()) {
            @Override
            protected int getLayoutRes() {
                return getItemLayoutRes();
            }

            @Override
            public void bindView(int position, @NonNull View convertView) {
                bindItemView(position, convertView);
            }
        };
    }

    /**
     * 其他操作
     */
    @Override
    protected void initData() {
        onRefresh();
    }

    protected String mPage;

    /**
     * 刷新, no refresh icon
     */
    @Override
    protected void refresh() {
        mPage = "0";
        loadMore();
    }

    /**
     * Callback method to be invoked when an item in this AdapterView has
     * been clicked.
     * <p>
     * Implementers can call getItemAtPosition(position) if they need
     * to access the data associated with the selected item.
     *
     * @param parent   The AdapterView where the click happened.
     * @param view     The view within the AdapterView that was clicked (this
     *                 will be a view provided by the adapter)
     * @param position The position of the view in the adapter.
     * @param id       The row id of the item that was clicked.
     */
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        try {
            onItemClick(mAdapter.getItem(position));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void onItemClick(T t) {
    }

    public T getLast() {
        List<T> data = mAdapter.getData();
        if (data.size() > 0) {
            return data.get(data.size() - 1);
        }
        return null;
    }


    public static <T extends View> T findViewById(View view, int id) {
        return BaseListAdapter.findViewById(view, id);
    }


    public static void fastText(View convertView, int[] ids, String[] values) {
        BaseListAdapter.fastText(convertView, ids, values);
    }

    public static void fastText(View convertView, int id, String value) {
        BaseListAdapter.fastText(convertView, id, value);
    }

    public static void fastOnClick(View convertView, int id, View.OnClickListener onClickListener) {
        BaseListAdapter.fastOnClick(convertView, id, onClickListener);
    }

    public static void fastVisibility(View convertView, int id, int visibility) {
        BaseListAdapter.fastVisibility(convertView, id, visibility);
    }
}
